package com.lsk.alipay.alipaylife;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayOpenPublicMenuCreateModel;
import com.alipay.api.domain.ButtonObject;
import com.alipay.api.request.AlipayOpenPublicMenuCreateRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.request.AlipayUserUserinfoShareRequest;
import com.alipay.api.response.AlipayOpenPublicMenuCreateResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.api.response.AlipayUserUserinfoShareResponse;
import com.jfinal.core.Controller;
import com.lsk.alipay.constants.AlipayServiceEnvConstants;
import com.lsk.alipay.factory.AlipayAPIClientFactory;
import com.lsk.alipay.util.RequestUtil;
import com.lsk.util.ReturnData;
import com.lsk.util.Syslog;

import java.util.*;

public class AlipaylifeController extends Controller {
    public void bb() throws AlipayApiException {
        Map<String,String> maps = new HashMap<String ,String>();
        //页面回调地址 必须与应用中的设置一样
        String return_url = "http://dingshang.iok.la/apply/authorization";
        //回调地址必须经encode
        return_url = java.net.URLEncoder.encode(return_url);
        //重定向到授权页面
        redirect("redirect:"+AlipayConfig.ALIPAY_URL+"?app_id=" + AlipayConfig.APP_ID + "&scope=auth_user&redirect_uri=" + return_url);
//        render("redirect:"+AlipayConfig.ALIPAY_URL+"?app_id=" + AlipayConfig.APP_ID + "&scope=auth_user&redirect_uri=" + return_url);

    }

    public void authorization() throws AlipayApiException {

        //获取支付宝GET过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map requestParams = getRequest().getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == (values.length-1)) ? valueStr + values[i]:valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }

        String accessToken= AlipaySubmit.buildRequest(params);
        if(accessToken!=null && accessToken!=""){
            String imf  =  AlipaySubmit.get(accessToken);
            System.out.println(imf);
        }

    }
    public void createMenu(){
        AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
        AlipayOpenPublicMenuCreateRequest request = new AlipayOpenPublicMenuCreateRequest();
        AlipayOpenPublicMenuCreateModel model = new AlipayOpenPublicMenuCreateModel();
        List<ButtonObject> list = new ArrayList<ButtonObject>();
        ButtonObject button = new ButtonObject();
        button.setName("邦邦售卡");
        button.setActionType("link");
        button.setActionParam("http://wxApplet.19xw.net/apply/login");
        list.add(button);
        model.setButton(list);
        request.setBizModel(model);
        AlipayOpenPublicMenuCreateResponse response = null;
        try {
            response = alipayClient.execute(request);
            System.out.println(response.getBody());
        } catch (Exception e) {
            Syslog.error(e);
            // TODO: handle exception
        }

    }



}
